package comp418.blingo;



import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;

import messagePackage.QuestionMessage;
import messagePackage.ReturningMessage;
import android.content.Context;
import android.util.Log;

/*
 * Coding by Liu Jingbang 10821473d, Hong kong polyu
 * */

public class Utility {
	private static Utility utility;
	private String username;
	private String password;
	private String email;
	private boolean english;
	private int coin;
	private int level;
	private int qid;
	static Thread temp;
	private Socket socket;
	private int serverport;
	public String server_ip=null;
	

	
	private SocketWriter WriterThread;
	private QuestionSocketReader QuestionReaderThread;
	private ReturningSocketReader ReturningReaderThread;
	
	ReturningMessage readMessage=null;
	QuestionMessage question=null;
	    
	ObjectInputStream in = null;
	ObjectOutputStream out = null;
	
	public Utility(String ip){
		server_ip=ip;
		temp=new Connection();
		temp.start();
	}
	public Utility(){
		
		temp=new Connection();
		temp.start();
	}
	public static Utility getUtility(String ip) {
		if (utility == null) {
			utility = new Utility(ip);
		}
		
		return utility;
	}
	public static Utility getUtility() {
		if (utility == null) {
			utility = new Utility();
		}
		
		return utility;
	}
	 public void showToast(Context context, String string){
	    	ToastHelper.showToast(context, string);
	    }

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isEnglish() {
		return english;
	}

	public void setEnglish(boolean english) {
		this.english = english;
	}

	public int getCoin() {
		return coin;
	}

	public void setCoin(int coin) {
		this.coin = coin;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getQid() {
		return qid;
	}

	public void setQid(int qid) {
		this.qid = qid;
	}
	
	
	public Boolean login(String username, String password) {
		WriterThread = new SocketWriter(out);
		WriterThread.setLogin(username,password);
		WriterThread.start();
		Boolean str=false;
		ReturningReaderThread.start();
		while(readMessage==null)
			readMessage =ReturningReaderThread.readMessage;
		str=readMessage.successful;
		readMessage=null;
		return str;
	}
	
	public Boolean signUp(String username, String password){
		WriterThread = new SocketWriter(out);
		if(WriterThread!=null)
			Log.i("ClientService", "unnull");
		WriterThread.setSignup(username, password);
		WriterThread.start();
		Boolean str=false;
		ReturningReaderThread.start();
		while(readMessage==null)
			readMessage =ReturningReaderThread.readMessage;
		str=readMessage.successful;
		readMessage=null;

		return str;
	}
	
	public ArrayList<QuestionShow> showList(){
		WriterThread=new SocketWriter(out);
		WriterThread.setQuestionList();
		WriterThread.start();
		ArrayList<QuestionShow> qs=new ArrayList();
		int qn=0;
		ReturningReaderThread=new ReturningSocketReader(in);
		ReturningReaderThread.start();
		while(readMessage==null)
			readMessage =ReturningReaderThread.readMessage;
		qn=readMessage.questionNum;
		
		readMessage=null;
		
		for(int i=0;i<qn;i++){
			if(i==0)
				QuestionReaderThread.start();
				
			else{
				QuestionReaderThread=new QuestionSocketReader(in);
				QuestionReaderThread.start();
			}
			while(question==null)
				question=QuestionReaderThread.readMessage;
			QuestionShow t=new QuestionShow(question.title,null,null,question.id);
			Log.i("tt",question.title);
			question=null;
			qs.add(t);
		}
		return qs;
	}
	
	public int RaiseQuestion(String title, String content){
		WriterThread=new SocketWriter(out);
	
		WriterThread.setCreateQuestion(title, content, this.username);
		WriterThread.start();
		int qid=0;
		ReturningReaderThread=new ReturningSocketReader(in);
		ReturningReaderThread.start();
		while(readMessage==null)
			readMessage =ReturningReaderThread.readMessage;
		qid=readMessage.questionID;
		readMessage=null;
		return qid;
	}
	public boolean answerQuestion( String content,int questionid){
		WriterThread=new SocketWriter(out);
		
		WriterThread.setCreateAnswer(questionid,  this.username,content);
		WriterThread.start();
		boolean result=false;
		ReturningReaderThread=new ReturningSocketReader(in);
		ReturningReaderThread.start();
		while(readMessage==null)
			readMessage =ReturningReaderThread.readMessage;
		result=readMessage.successful;
		readMessage=null;
		return result;
	}
	
	public QuestionShow OpenQuestion(int qid){

		WriterThread=new SocketWriter(out);
		
		WriterThread.setQuestion(qid);
		WriterThread.start();
		QuestionShow q=null;
		ReturningReaderThread=new ReturningSocketReader(in);
		ReturningReaderThread.start();
		Log.i("test:","here,openquestion");
		while(readMessage==null)
			readMessage =ReturningReaderThread.readMessage;
		q=new QuestionShow(readMessage.questionTitle,readMessage.questionTextContent,readMessage.answerTextContent,readMessage.questionID);
		
		Log.i("question",readMessage.questionTitle);
		Log.i("question",readMessage.questionTextContent);
		readMessage=null;
		return q;
	}
	public boolean GiveCoin(int qid){
		WriterThread=new SocketWriter(out);
		
		WriterThread.setCoin(qid);
		WriterThread.start();
		boolean result=false;
		ReturningReaderThread=new ReturningSocketReader(in);
		ReturningReaderThread.start();
		while(readMessage==null)
			readMessage =ReturningReaderThread.readMessage;
		result=readMessage.successful;
		readMessage=null;
		return result;
	}
	public int getProfile(String username){
		WriterThread=new SocketWriter(out);
		WriterThread.setUserprofile(username);
		WriterThread.start();
		int result=0;
		ReturningReaderThread=new ReturningSocketReader(in);
		ReturningReaderThread.start();
		while(readMessage==null)
			readMessage =ReturningReaderThread.readMessage;
		result=readMessage.questionNum;
		return result;
	}
	class Connection extends Thread {
		public Connection() {
		}
		public void run() {
			serverport = 1300;

			server_ip="192.168.22.10";
		
			InetAddress serverAddr;
			try {
				serverAddr = InetAddress.getByName(server_ip);
				Log.i("ClientService", "coasdfnnect");
				
				socket = new Socket(serverAddr, serverport);
				Log.i("ClientService", "connect");
			
					if (socket.isConnected()) {

						try {
							out = new ObjectOutputStream(
									socket.getOutputStream());
							in = new ObjectInputStream(socket.getInputStream());
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						Log.i("ClientService", "unnull");
						WriterThread = new SocketWriter(out);
						ReturningReaderThread=new ReturningSocketReader(in);
						QuestionReaderThread=new QuestionSocketReader(in);
						
						
						
					}

			} catch (Exception e) {
				Log.i("ClientService", "uuugh");

			}

		}

	}
}
